import { get } from "lodash";
import { parse } from "./index";

export default class Motion {
    constructor(points, duration) {
        this.bezierList = parse(points);
        if (this.bezierList.length <= 0) {
            return;
        }

        this.t = 0;
        this.moving = false;
        this.speed = (1 * this.bezierList.length) / duration;

        this.onCompleted = null;
    }

    start() {
        this.moving = true;
        this.t = 0;
    }

    stop() {
        this.moving = false;
        this.onCompleted && this.onCompleted();
    }

    update(dt) {
        if (!this.moving) {
            return;
        }

        this.t += this.speed * dt;
        const bezier = this.bezierList[Math.floor(this.t)];
        if (bezier) {
            return bezier.getPos(this.t % 1);
        } else {
            this.stop();
            return null;
        }
    }
}